package com.cqie.practicaltraining.config.util;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;

import java.io.IOException;

public class IntegerDeserializer extends JsonDeserializer<Integer> {
    @Override
    public Integer deserialize(JsonParser p, DeserializationContext text) throws IOException, JsonProcessingException {
        String value = p.getText();
        try {
            int intValue = Integer.parseInt(value);
            if (intValue != 0 && intValue != 1) {
                throw new IllegalArgumentException("状态只能为0或1");
            }
            return intValue;
        } catch (NumberFormatException e) {
            throw new IllegalArgumentException("状态必须是一个有效的整数");
        }
    }
}
